    //
//  FeatureMembersController.m
//  SpendFunk
//
//  Created by bm on 4/19/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "FeatureMembersController.h"
#import "SpendFunkAppDelegate.h"
#import "FeaturedMemberCell.h"
#import "Global.h"
#import "image.h"

//NSMutableArray *m_pArrMembers;
@implementation FeatureMembersController

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)init{	
	m_pDictUserInfo = [[NSMutableDictionary alloc] init];
	return self;
}

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
	[super loadView];
	
	[self initView];
}

-(void)initView{
	//back
	NSString *path = nil;
	UIImage *img = nil;
	
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BKGR_VIEW];
	img = [UIImage imageNamed:path];
	UIImageView *imgvFFBkgr = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 320, 480)];
	[imgvFFBkgr setImage:img];
	[self.view addSubview:imgvFFBkgr];
	[imgvFFBkgr setUserInteractionEnabled:YES];
	[imgvFFBkgr release];
	
	[path release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BTN_BACK];
	img = [UIImage imageNamed:path];	
	m_pBtnBack = [[UIButton alloc] initWithFrame:CGRectMake(5, -14, img.size.width, img.size.height)];
	[m_pBtnBack setBackgroundImage:img forState:UIControlStateNormal];
	[m_pBtnBack setBackgroundColor:[UIColor clearColor]];
	[m_pBtnBack setTitle:@"Back" forState:UIControlStateNormal];
	m_pBtnBack.titleLabel.textColor = [UIColor whiteColor];
	[m_pBtnBack addTarget:nil action:@selector(onClickBack) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:m_pBtnBack];
	
	///table
	int nHRow = 45;
	m_pMainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 80, 320, 5*nHRow) style:UITableViewStylePlain];
	m_pMainTableView.delegate = self;
	m_pMainTableView.dataSource = self;
	m_pMainTableView.rowHeight = nHRow;
	[m_pMainTableView setShowsVerticalScrollIndicator:NO];
	[m_pMainTableView setBackgroundColor:[UIColor clearColor]];
	[self.view addSubview:m_pMainTableView];
	[m_pMainTableView reloadData];
	m_pMainTableView.scrollEnabled = YES;
}
-(void)setArrayMembers:(NSMutableArray *)members{

	if (m_pArrFriends) {
		[m_pArrFriends release];
		m_pArrFriends = nil;
	}
	m_pArrFriends = [members retain];
	[m_pMainTableView reloadData];
}

//-(void)setArrayMembers:(NSArray *)members{
//
//	if (g_iMode == MODE_SEARCHUSER) {
//		if (m_pArrFriends) {
//			[m_pArrFriends release];
//			m_pArrFriends = nil;
//		}
//		m_pArrFriends = [members retain];
//		
//	}else {
//		iCurrCount = 0;
//		iCountFriend = 0;
//		
//		if([members count]<1 || members==nil){
//			if(m_pArrMembers){
//				[m_pArrMembers release];
//				m_pArrMembers = nil;
//			}
//			//m_pArrMembers = [members retain];
//			return;
//		}
//		if(m_pArrMembers){
//			[m_pArrMembers release];
//			m_pArrMembers = nil;
//		}
//		m_pArrMembers = [members retain];
//		
//		NSLog(@"%s",[members objectAtIndex:0]);
//		NSString *strAllMembers = [[NSString alloc] initWithString:[members objectAtIndex:0]];
//		if([members count] == 1)
//		{
//			
//		}
//		else
//		{
//			for(int i = 1; i < [members count]; i++){
//				strAllMembers = [NSString stringWithFormat:@"%@,%@",strAllMembers,[members objectAtIndex:i]];
//			}
//			strAllMembers = [NSString stringWithFormat:@"%@,%@", g_strFacebookId, strAllMembers];
//		}
//		
//		//default g_iMode = 0
//		NSString *post = nil;
//		NSData *postData = nil;
//		NSString *postLength = nil; 
//		NSURL *url = nil;
//		if(g_iMode == MODE_CONTACT_LIST)
//		{
//			post = [NSString stringWithFormat:@"userid=%@&str_email=%@", g_strLoginUserId, strAllMembers];
//			postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];  
//			postLength = [NSString stringWithFormat:@"%d", [postData length]];
//			url = [NSURL URLWithString:@"http://demo.thejupitech.com/snt/spendfunk/api/users/contactlist"];
//		}
//		else if(g_iMode == MODE_FACEBOOK)
//		{
//			//post = [NSString stringWithFormat:@"userid=%@&str_facebookid=%@", g_strLoginUserId, strAllMembers];
//			//post = [NSString stringWithFormat:@"userid=%@&str_facebookid=100000596976260,2,345", g_strLoginUserId];
//			post = [NSString stringWithFormat:@"userid=9&str_facebookid=100000813954361,4,123456"];
//			postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];  
//			postLength = [NSString stringWithFormat:@"%d", [postData length]];
//			url = [NSURL URLWithString:@"http://demo.thejupitech.com/snt/spendfunk/api/users/facebookcontact"];
//		}
//		else if(g_iMode == MODE_TWITTER)
//		{
//			post = [NSString stringWithFormat:@"userid=%@&str_twitterid=%@", g_strLoginUserId, strAllMembers];
//			postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];  
//			postLength = [NSString stringWithFormat:@"%d", [postData length]];
//			url = [NSURL URLWithString:@"http://demo.thejupitech.com/snt/spendfunk/api/users/twittercontact"];
//		}
//		NSLog(@"%@",post);
//		
//		NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
//		[theRequest setHTTPMethod:@"POST"];  
//		[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];  
//		[theRequest setHTTPBody:postData];	
//		
//		NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
//		if( theConnection )
//		{
//			m_pWebData = [[NSMutableData data] retain];
//			isContinueParse = true;
//			
//			if(!m_pArrFriends){
//				m_pArrFriends = [[NSMutableArray alloc] init];
//			}else {
//				[m_pArrFriends removeAllObjects];
//			}
//			
//		}
//		else
//		{
//			
//		}
//	}
//
//	
//}
//
//#pragma mark delegate connection
//-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
//{
//	[m_pWebData setLength:0];
//}
//-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
//{
//	[m_pWebData appendData:data];
//}
//-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
//{
//	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cannot connect to internet" message:@"Please connect to internet!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
//	[alert show];
//	
//	[alert release];
//	[connection release];
//	[m_pWebData release];
//}
//-(void)connectionDidFinishLoading:(NSURLConnection *)connection
//{
//	NSData *loginStatus = [[NSData alloc] initWithBytes: [m_pWebData mutableBytes] length:[m_pWebData length]];
//	
//	NSString *a = [[NSString alloc] initWithData:loginStatus encoding:NSUTF8StringEncoding];
//	NSLog(@"%@",a);
//	
//	NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:loginStatus];
//	[xmlParser setDelegate:self];
//	
//	BOOL success = [xmlParser parse];
//	
//	if(success)
//	{
//		NSLog(@"No Errors");
//		
//	}
//	else
//	{
//		NSLog(@"Error Error Error!!!");
//	}
//	
//	[connection release];
//	[m_pWebData release];
//}
//
//#pragma mark delegate xmlParser
//- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
//  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
//	attributes:(NSDictionary *)attributeDict {
//	if(!currentElement) 
//		currentElement = [[NSString alloc] initWithString:elementName];
//	else
//		currentElement = [elementName retain];
//	
//	NSLog(@"Processing Element: %@", elementName);
//	
//	//iCurrCount = iCountFriend;
//	if([currentElement isEqualToString:kUserID]){
//		iCountFriend++;
//		if(iCountFriend >=2){
//			[m_pArrFriends addObject:m_pDictUserInfo];
//		}
//		[m_pDictUserInfo release];
//		m_pDictUserInfo = nil;
//		m_pDictUserInfo = [[NSMutableDictionary alloc] init];
//	}
//}
//
//- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
//	NSLog(@"Processing Value: %@", string);
//	
//	if([currentElement isEqualToString:kUserID]){
//		[m_pDictUserInfo setObject:string forKey:kUserID];
//	}
//	if([currentElement isEqualToString:kUserName]){
//		[m_pDictUserInfo setObject:string forKey:kUserName];
//	}
//	if([currentElement isEqualToString:kFirstName]){
//		[m_pDictUserInfo setObject:string forKey:kFirstName];
//	}
//	if([currentElement isEqualToString:kLastName]){
//		[m_pDictUserInfo setObject:string forKey:kLastName];
//	}
//	if([currentElement isEqualToString:kEmail]){
//		[m_pDictUserInfo setObject:string forKey:kEmail];
//	}
//	if([currentElement isEqualToString:kAvatar]){
//		[m_pDictUserInfo setObject:string forKey:kAvatar];
//	}
//	if([currentElement isEqualToString:kUserType]){
//		[m_pDictUserInfo setObject:string forKey:kUserType];
//	}
//	if([currentElement isEqualToString:kFacebookID]){
//		[m_pDictUserInfo setObject:string forKey:kFacebookID];
//	}
//	if([currentElement isEqualToString:kTwitterID]){
//		[m_pDictUserInfo setObject:string forKey:kTwitterID];
//	}
//	if([currentElement isEqualToString:kFollow]){
//		[m_pDictUserInfo setObject:string forKey:kFollow];
//	}
//}
//
//- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
//  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
//	
//	[currentElement release];
//	currentElement = nil;
//	[currentElementValue release];
//	currentElementValue = nil;
//	
//	if([elementName isEqualToString:@"xml"]){
//		[m_pMainTableView reloadData];
//	}
//}

-(void)onClickBack{
	[g_AppDelegate openFindFriends];
}


#pragma mark tableViewdelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	if (m_pArrFriends) {
		return [m_pArrFriends count];
	}
	else{
		return 0;
	}
}

#pragma mark note
/*
 Can nhan biet khi nao tra ve array khi ma scan contaclist, hay facebook, hay twiiter, search for user
 */

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	FeaturedMemberCell *cell = nil;
	cell = (FeaturedMemberCell*)[tableView dequeueReusableCellWithIdentifier:@"3"];
	if (cell == nil)
	{
		cell = [[[FeaturedMemberCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"3"] autorelease];
	}
	
	// Demo
	NSString *sImg = [[NSString alloc] initWithFormat:@""];
	cell.imageView.image = [UIImage imageNamed:sImg];
	[sImg release];
	
	[cell setLabelName:[[m_pArrFriends objectAtIndex:indexPath.row] objectForKey:kUserName]];
	[cell setUserId:[[m_pArrFriends objectAtIndex:indexPath.row] objectForKey:kUserID]];
	
	NSString *followerState = [[m_pArrFriends objectAtIndex:indexPath.row] objectForKey:kFollow];
	if([followerState isEqualToString:@"true"]){
		[cell setFollower:TRUE];
	}else{
		[cell setFollower:FALSE];
	}
	
	// Twitter 
	//NSString *str = [[m_pArrMembers objectAtIndex:indexPath.row] valueForKey:@"profile_image_url"];
//	NSData *receivedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]];
//	UIImage *image = [[UIImage alloc] initWithData:receivedData] ;
//	[cell.imageView setImage:image];
//	[cell setLabelName:[[m_pArrMembers objectAtIndex:indexPath.row] valueForKey:@"name"]];
//	[image release];
	
	// Facebook
	
	
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	[tableView	deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
